﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Negocios;

public partial class ManterGrupoVideo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
   
    protected void btNovo_Click(object sender, EventArgs e)
    {
        tbNome.Enabled = true;
        tbDescricao.Enabled = true;
        cbCompartilhado.Enabled = true;
        tbUsuario.Enabled = true;
        btGravar.Enabled = true;
        btCancelar.Enabled = true;
        btNovo.Enabled = true;
        tbUsuario.Enabled = true;
    }
    private void cancelar()
    {
        tbCodigo.Text = "";
        tbNome.Text = "";
        tbDescricao.Text = "";
        cbCompartilhado.Checked = false;
        tbUsuario.Text = "";


        tbNome.Enabled = false;
        tbDescricao.Enabled = false;
        cbCompartilhado.Enabled = false;
        tbUsuario.Enabled = false;
        btGravar.Enabled = false;
        btCancelar.Enabled = false;
        btNovo.Enabled = true;

    }
    protected void btGravar_Click(object sender, EventArgs e)
    {
        Session["usuario"] = "teste";

        try
        {
            if (tbCodigo.Text == "")
            {
                GrupoDeVideosBLL aBLL = new GrupoDeVideosBLL();
                GrupoDeVideos g = new GrupoDeVideos(tbNome.Text, tbDescricao.Text, Session["usuario"].ToString(), cbCompartilhado.Checked);
                aBLL.Insere(g);
            }
            else
            {
                GrupoDeVideosBLL aBLL = new GrupoDeVideosBLL();
                GrupoDeVideos g = new GrupoDeVideos(Convert.ToInt32(tbCodigo.Text), tbNome.Text, tbDescricao.Text, Session["usuario"].ToString(), cbCompartilhado.Checked);
                aBLL.Atualiza(g);
            }
            GridView1.DataBind();
            cancelar();
        }
    catch(Exception ex){
        lbErro.Text = ex.Message;
}
    }

    protected void btCancelar_Click(object sender, EventArgs e)
    {
        cancelar();

    }
    protected void lbRemove_Click(object sender, EventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text);
            GrupoDeVideosBLL grupoDeVideos = new GrupoDeVideosBLL();
            GrupoDeVideos g = new GrupoDeVideos(id);
            grupoDeVideos.Remove(g);
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }
    protected void lbAtualiza_Click(object sender, EventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text);
            GrupoDeVideosBLL grupoDeVideos = new GrupoDeVideosBLL();
            GrupoDeVideos g = grupoDeVideos.Consulta(id);

            tbCodigo.Text = g.Codigo.ToString();
            tbDescricao.Text = g.Descricao;
            tbNome.Text = g.Nome;
            cbCompartilhado.Checked = g.Compartilhado;
            //incluir usuario depois;

            tbNome.Enabled = true;
            cbCompartilhado.Enabled = true;
            tbDescricao.Enabled = true;
            btCancelar.Enabled = true;
            btGravar.Enabled = true;
            btNovo.Enabled = false;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }
}
